package com.kly.user.db.dao.impl;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserMsgDao;
import com.kly.user.db.dbo.SumUserMsgDBO;
import com.kly.user.db.dbo.UserMessageCountDBO;
import com.kly.user.db.dbo.UserMsgDBO;
import com.kly.user.db.entity.UserMsgEntity;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* User: linqu
* Date: 2023-05-24
* Time: 10:10:36.873
*/

@Repository
public class UserMsgDaoImpl extends AbstractBaseMapper<UserMsgEntity> implements UserMsgDao {

    @Override
    public List<UserMsgEntity> selectByFromUserAndToUser(Map<String, Object> map) {
        return getSqlSession().selectList(this.getStatement("selectByFromUserAndToUser"), map);
    }

    @Override
    public List<UserMsgEntity> listTodayMessageWithoutSession() {
        return getSqlSession().selectList(this.getStatement("listTodayMessageWithoutSession"));
    }

    @Override
    public List<UserMsgEntity> selectByUserCode(String fromUserCode, String toUserCode) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("fromUserCode", fromUserCode);
        params.put("toUserCode", toUserCode);
        return getSqlSession().selectList(this.getStatement("selectByUserCode"), params);
    }

    @Override
    public List<SumUserMsgDBO> listUserMsgByFromUserCode(Map<String, Object> map) {
        return getSqlSession().selectList(this.getStatement("listUserMsgByFromUserCode"), map);
    }

    @Override
    public List<UserMsgEntity> selectTodayMessages(Map<String, Object> map) {
        return getSqlSession().selectList(this.getStatement("selectTodayMessages"), map);
    }

    @Override
    public int selectByCreateDate(Map<String, Object> map) {
        return getSqlSession().selectOne(this.getStatement("selectByCreateDate"), map);
    }

    @Override
    public List<SumUserMsgDBO> getUserMsgByFromUserCode(Map<String, Object> var) {
        return getSqlSession().selectList(this.getStatement("getUserMsgByFromUserCode"), var);
    }

    @Override
    public List<UserMsgDBO> selectBySessionIds(List<String> sessionIds, Date todayBeginning) {
        final HashMap<String, Object> params = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(sessionIds)) {
            params.put("list", sessionIds);
        }
        params.put("todayBeginning", todayBeginning);
        return this.getSqlSession().selectList(this.getStatement("selectBySessionIds"), params);
    }

    @Override
    public List<UserMsgDBO> getAllUserMsgBySessionId(String sessionId, Date todayBeginning) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("sessionId", sessionId);
        params.put("todayBeginning", todayBeginning);
        return this.getSqlSession().selectList(this.getStatement("getAllUserMsgBySessionId"), params);
    }

    @Override
    public List<UserMsgDBO> listTodaySessions(Date todayBeginning) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("todayBeginning", todayBeginning);
        return this.getSqlSession().selectList(this.getStatement("listTodaySessions"), params);
    }

    @Override
    public Integer updateReplyTimes(Long id, Long replyTimes) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("id", id);
        params.put("replyTimes", replyTimes);
        return this.getSqlSession().update(this.getStatement(".updateReplyTimes"), params);
    }

    @Override
    public Integer updateChatterReplyTimes(String fromUserCode, String toUserCode, int replyTimes) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("fromUserCode", fromUserCode);
        params.put("toUserCode", toUserCode);
        params.put("replyTimes", replyTimes);
        return this.getSqlSession().update(this.getStatement("updateChatterReplyTimes"), params);
    }

   @Override
    public Integer updateReplyTimesIfChatterReplied(String fromUserCode, String toUserCode) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("fromUserCode", fromUserCode);
        params.put("toUserCode", toUserCode);
        return this.getSqlSession().update(this.getStatement("updateReplyTimesIfChatterReplied"), params);
    }

    @Override
    public Integer selectTodayMessageCount(String modelUserCode){
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("modelUserCode", modelUserCode);
        return (Integer)this.getSqlSession().selectOne(this.getStatement("selectTodayMessageCount"), params);
    }

    @Override
    public Integer querySessionsByModelUserCode(String modelUserCode, Integer queryTodayFlag) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("modelUserCode", modelUserCode);
        if (queryTodayFlag != null) {
            params.put("queryTodayFlag", queryTodayFlag);
        }
        return (Integer)this.getSqlSession().selectOne(this.getStatement("querySessionsByModelUserCode"), params);
    }

    @Override
    public List<UserMessageCountDBO> selectTodayMessageCountInfo(String chatterCode,String modelUserCode, Integer startCount){
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("chatterCode", chatterCode);
        params.put("modelUserCode", modelUserCode);
        params.put("startCount", startCount);
        return this.getSqlSession().selectList(this.getStatement("selectTodayMessageCountInfo"), params);
    }

    @Override
    public List<String> selectModelCodes(String chatterCode){
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("chatterCode", chatterCode);
        return this.getSqlSession().selectList(this.getStatement("selectModelCodes"), params);
    }

    @Override
    public Integer selectMessageCount(String userCode){
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return (Integer)this.getSqlSession().selectOne(this.getStatement("selectMessageCount"), params);
    }

    @Override
    public Integer removeMsg() {
        return this.getSqlSession().delete(this.getStatement("removeMsg"));
    }

    @Override
    public Integer backup() {
        return this.getSqlSession().insert(this.getStatement("backup"));
    }

    @Override
    public UserMsgEntity getLatestMsg(String sessionId) {
        return this.getSqlSession().selectOne(this.getStatement("getLatestMsg"), sessionId);
    }
}